<script lang="ts" setup>
import { ref, watch } from 'vue'

interface SelectOption {
  value: string
  label: string
}

interface Props {
  modelValue: string
  options: SelectOption[]
  placeholder?: string
  disabled?: boolean
}

interface Emits {
  (e: 'update:modelValue', value: string): void
  (e: 'change', value: string, option: SelectOption): void
}

const props = withDefaults(defineProps<Props>(), {
  placeholder: '请选择',
  disabled: false
})

const emit = defineEmits<Emits>()

// 控制下拉框显示/隐藏
const isExpanded = ref(false)

// 当前选中的值
const selectedLabel = ref('')

// 监听modelValue变化
watch(() => props.modelValue, (newValue) => {
  const option = props.options.find(opt => opt.value === newValue)
  selectedLabel.value = option ? option.label : ''
}, { immediate: true })

// 切换下拉框状态
const toggleDropdown = () => {
  if (props.disabled) return
  isExpanded.value = !isExpanded.value
}

// 选择选项
const selectOption = (option: SelectOption) => {
  selectedLabel.value = option.label
  emit('update:modelValue', option.value)
  emit('change', option.value, option)
  isExpanded.value = false
}

// 点击外部关闭下拉框
const handleClickOutside = () => {
  isExpanded.value = false
}
</script>

<template>
  <view class="custom-select" :class="{ disabled, expanded: isExpanded }">
    <!-- 选择器触发区域 -->
    <view class="select-trigger" @click="toggleDropdown">
      <view class="select-content">
        <text class="select-text" :class="{ placeholder: !selectedLabel }">
          {{ selectedLabel || placeholder }}
        </text>
        <i class="fas fa-chevron-down select-arrow" :class="{ rotated: isExpanded }"></i>
      </view>
    </view>

    <!-- 下拉选项列表 -->
    <view v-if="isExpanded" class="select-dropdown" @click.stop>
      <view class="dropdown-content">
        <view
          v-for="option in options"
          :key="option.value"
          class="select-option"
          :class="{ selected: option.value === modelValue }"
          @click="selectOption(option)"
        >
          <text class="option-text">{{ option.label }}</text>
          <i v-if="option.value === modelValue" class="fas fa-check option-check"></i>
        </view>
      </view>
    </view>

    <!-- 遮罩层 -->
    <view
      v-if="isExpanded"
      class="select-overlay"
      @click="handleClickOutside"
      @touchmove.prevent
    ></view>
  </view>
</template>

<style lang="scss" scoped>
.custom-select {
  position: relative;
  width: 100%;
  height: 80rpx;
  background-color: var(--background-light);
  border: 2rpx solid var(--border-color);
  border-radius: var(--border-radius);
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.custom-select.expanded {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 4rpx rgba(255, 107, 53, 0.1);
}

.custom-select.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.select-trigger {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.select-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 100%;
  padding: 0 var(--spacing-md);
}

.select-text {
  flex: 1;
  font-size: 28rpx;
  color: var(--text-primary);
  line-height: 1.4;
}

.select-text.placeholder {
  color: var(--text-muted);
}

.select-arrow {
  font-size: 24rpx;
  color: var(--text-muted);
  transition: transform 0.3s ease;
  margin-left: var(--spacing-sm);
}

.select-arrow.rotated {
  transform: rotate(180deg);
}

// 下拉选项样式
.select-dropdown {
  position: absolute;
  top: calc(100% + 8rpx);
  left: 0;
  right: 0;
  z-index: 1000;
  background-color: var(--background-medium);
  border: 2rpx solid var(--border-color);
  border-radius: var(--border-radius);
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.3);
  overflow: hidden;
  animation: dropdownSlide 0.2s ease-out;
}

@keyframes dropdownSlide {
  from {
    opacity: 0;
    transform: translateY(-8rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.dropdown-content {
  max-height: 400rpx;
  overflow-y: auto;
}

.select-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-md);
  cursor: pointer;
  transition: background-color 0.2s ease;
  border-bottom: 1rpx solid var(--border-color);
}

.select-option:last-child {
  border-bottom: none;
}

.select-option:hover,
.select-option:active {
  background-color: rgba(255, 107, 53, 0.1);
}

.select-option.selected {
  background-color: rgba(255, 107, 53, 0.15);
  color: var(--primary-color);
}

.option-text {
  flex: 1;
  font-size: 28rpx;
  color: var(--text-primary);
  line-height: 1.4;
}

.select-option.selected .option-text {
  color: var(--primary-color);
  font-weight: 500;
}

.option-check {
  font-size: 24rpx;
  color: var(--primary-color);
  margin-left: var(--spacing-sm);
}

// 遮罩层
.select-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  background: transparent;
}
</style>